package com.yijinsuo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yijinsuo.R;
import com.yijinsuo.base.BaseActivity;
import com.yijinsuo.entity.TiXianHuifu;
import com.yijinsuo.http.HttpCallBack;
import com.yijinsuo.http.HttpUtils;
import com.yijinsuo.mian.Config;
import com.yijinsuo.response.TiXianRes;
import com.yijinsuo.response.TiXianWithdrawRes;
import com.yijinsuo.tool.VibratorUtil;
import com.yijinsuo.util.HtmlFormUtil;
import com.yijinsuo.view.ClearEditText;
import com.yijinsuo.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 账户提现
 */
public class AccountTiXianActivity extends BaseActivity {
    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_money_number)
    ClearEditText etMoneyNumber;
    @Bind(R.id.tv_AvaTotal)
    TextView tvAvaTotal;
    //
    private TiXianRes info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_account_ti_xian);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {

        titlebar.setTitleBar(R.drawable.back_selector, "账户提现", 0);


    }

    @Override
    protected void initView() {

        info = (TiXianRes) getIntent().getSerializableExtra("info");
    }

    @Override
    protected void setData() {
        if (null != info) {

            tvAvaTotal.setText(info.getAvaTotal());
        }

    }


    @OnClick({R.id.titlebar_iv_back, R.id.tv_tixian})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:

                finish();

                break;


            case R.id.tv_tixian:     //提现
                long[] pattern = {100, 400, 100, 400};
                String moneyNumber = etMoneyNumber.getText().toString().trim();
                String max = info.getTxmaxFee();
                String min = info.getTxminFee();
                float floatMax = Float.parseFloat(max);
                float floatMin = Float.parseFloat(min);

                if (TextUtils.isEmpty(moneyNumber)) {
                    etMoneyNumber.setShakeAnimation();
                    VibratorUtil.Vibrate(AccountTiXianActivity.this, pattern, false);
                    Toast.makeText(AccountTiXianActivity.this, "提现金额不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }
                if ((Float.parseFloat(etMoneyNumber.getText().toString().trim())<floatMin)) {
                    etMoneyNumber.setShakeAnimation();
                    Toast.makeText(AccountTiXianActivity.this, "最小提现金额:" + info.getTxminFee(), Toast.LENGTH_SHORT).show();
                    break;
                }

//                if((Float.parseFloat(etMoneyNumber.getText().toString().trim())>floatMax)){
//                    etMoneyNumber.setShakeAnimation();
//                    Toast.makeText(AccountTiXianActivity.this, "最大提现金额:" + info.getTxmaxFee(), Toast.LENGTH_SHORT).show();
//                }


                //
                getdata();




                break;


            default:
        }
    }


    /**
     *
     * 确认体现
     */
    private void getdata() {
        HttpParams params=new HttpParams();
        params.put("takeMoney",etMoneyNumber.getText().toString().trim());
        HttpUtils.post(Config.WITHDRAW, params, new HttpCallBack(this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if(null!=result){
                    try {
                        TiXianWithdrawRes res=new TiXianWithdrawRes(new JSONObject(result.toString()));
                        TiXianHuifu info=null;
                        if(null!=res.getHuifu()){
                            info=res.getHuifu();
                            Map<String ,String> params=new HashMap<String, String>();
                            params.put("login_id",info.getLoginId());
                            params.put("mchnt_cd",info.getMchntCd());
                            params.put("back_notify_url",info.getBackNotifyUrl());
                            params.put("back_notify_url",info.getBackNotifyUrl());
                            params.put("amt",info.getAmt());
                            params.put("page_notify_url",info.getPageNotifyUrl());
                            params.put("mchnt_cd",info.getMchntCd());
                            try {
                                params.put("signature", URLEncoder.encode(info.getSignature(),"UTF-8"));
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            params.put("mchnt_txn_ssn",info.getMchntTxnSsn());
                            Intent intent=new Intent(AccountTiXianActivity.this,WebActivity.class);
                            intent.putExtra("url",info.getGateWayUrl());
                            intent.putExtra("postData", HtmlFormUtil.makePostHTML(info.getGateWayUrl(),params));
                            intent.putExtra("titile_text","确认提现");
                            try {
                                intent.putExtra("params", HtmlFormUtil.paramMap2bytes(params));
                                startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(AccountTiXianActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });


    }


    @Override
    protected void onResume() {
        super.onResume();
        sendToWithdraw();



    }

    private void sendToWithdraw() {

        HttpParams params=new HttpParams();

        HttpUtils.post(Config.TOWITHDRAW, params, new HttpCallBack(this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if(null!=result){
                    try {
                        TiXianRes res=new TiXianRes(new JSONObject(result.toString()));
                        if(null!=res){
                            tvAvaTotal.setText(res.getAvaTotal());
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onError(String code, String message) {
            }
        });

    }
}
